package com.example.day0824_ui.slice;

import com.example.day0824_ui.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
import ohos.agp.window.dialog.ToastDialog;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.multimodalinput.event.TouchEvent;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class MainAbilitySlice extends AbilitySlice {

    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP,0x00201,"MY_TAG");

    private static TextField tf1;           // 账号框
    private static TextField tf2;           // 密码框
    private static Text see_passw;          // 查看密码
    private static Button btn_login;        //  登录框
    private static Button btn_register;     //  注册框

    private static final String ADMI = "lihuan"; // 管理员账号
    private static final String PASSW = "666";   // 管理员密码

    private static final Set<String[]> ADMI_PASSW = new HashSet(); // 注册的账号密码

    private final static String BUNDLENAME = "com.example.day0824_ui";


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        BtnSet();
    }
    private void BtnSet() {
        tf1 = (TextField)findComponentById(ResourceTable.Id_textf_admi);
        tf2 = (TextField)findComponentById(ResourceTable.Id_textf_psw);
        see_passw = (Text)findComponentById(ResourceTable.Id_see_psw);
        btn_login = (Button)findComponentById(ResourceTable.Id_btn_login);
        btn_register = (Button)findComponentById(ResourceTable.Id_btn_register);


        see_passw.setTouchEventListener(this::onTouchEven);
        btn_login.setClickedListener(component -> getLogReslut("com.example.day0824_ui.slice.Login_Ability"));
        btn_register.setClickedListener(component -> JumpAbility("com.example.day0824_ui.slice.RegisterAbility"));
    }

//    获取 文本框内的值 进行登录认证
    private void getLogReslut(String abilityName) {
        String admi = tf1.getText();
        String passw = tf2.getText();
        String[] strap = new String[2];
        strap[0] = tf1.getText();
        strap[1] = tf2.getText();
        if ((admi.equals(ADMI)  && passw.equals(PASSW)) || ADMI_PASSW.contains(strap)) {
            JumpLogAbility(abilityName);
        }else {
            new ToastDialog(this).setText("账号或密码错误").show();
        }


    }

    // 查看密码
    private boolean onTouchEven(Component component, TouchEvent touchEvent) {
        int action = touchEvent.getAction();
        //按下不松的时候
        if(action == TouchEvent.PRIMARY_POINT_DOWN){
            tf2.setTextInputType(InputAttribute.PATTERN_NULL);
        }else if(action == TouchEvent.PRIMARY_POINT_UP){
            //松开的时候
            tf2.setTextInputType(InputAttribute.PATTERN_PASSWORD);
        }

    return true;
    }
    // 跳转 登录
    private void JumpLogAbility(String abilityName) {
        Intent it = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(BUNDLENAME)
                .withAbilityName(abilityName)
                .build();
        it.setOperation(operation);
        it.setParam("value",tf1.getText());
        startAbility(it);
    }
    // 跳转 注册
    private void JumpAbility(String abilityName) {
        HiLog.info(LABEL,"1");
        Intent it = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(BUNDLENAME)
                .withAbilityName(abilityName)
                .build();
        it.setOperation(operation);
        startAbility(it);

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
